//
//  menuView.m
//  78787878
//
//  Created by Mac on 16/4/17.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "YJPopMenuView.h"
//缩放比例
#define tableViewScale  0.4
//动画时长
#define animationTime  0.15
#define BACKCOLOVALUE 0xffffff
@interface YJPopMenuView()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_titles;
    NSArray *_images;
}
@property(nonatomic,weak)UITableView *tableView;
//保存显示的位置，用来设置三角
@property(nonatomic,assign)CGPoint point;
@end

@implementation YJPopMenuView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles images:(NSArray *)images
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _titles = titles;
        _images = images;
        
       UIWindow *win = [UIApplication sharedApplication].windows.lastObject;
        //整个view的背景色
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        //当前view大小
        self.frame = win.bounds;
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.backgroundColor = kUIColorFromRGB(BACKCOLOVALUE);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //表格分割线，tablewView与cell中的方法都要实现
        if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
            tableView.separatorInset = UIEdgeInsetsMake(0, -20, 0, 0);
        if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
            tableView.layoutMargins = UIEdgeInsetsZero;
        //分割线颜色
        tableView.separatorColor = kUIColorFromRGB(0x3c4355);
        //动画开始点
        tableView.layer.anchorPoint = CGPointMake(1, 0);

        tableView.scrollEnabled = NO;
        tableView.layer.cornerRadius = 8;
        tableView.layer.masksToBounds = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        [self addSubview:tableView];
        self.tableView = tableView;
    }
    return self;
}

#pragma mark ------------------------ 画三角
- (void)drawRect:(CGRect)rect
{
    //1.获取当前图形上下文
    CGContextRef ct = UIGraphicsGetCurrentContext();
    //2.添加描述路径
    CGContextMoveToPoint(ct, _point.x, _point.y);
    CGContextAddLineToPoint(ct, _point.x - 9, _point.y + 10);
    CGContextAddLineToPoint(ct, _point.x + 9, _point.y + 10);
    //线条颜色、填充的颜色
    [kUIColorFromRGB(BACKCOLOVALUE) setStroke];
    [kUIColorFromRGB(BACKCOLOVALUE) set];
    //3.渲染
    CGContextDrawPath(ct, kCGPathFillStroke);
}

//显示菜单
- (void)showFrom:(CGPoint)point
{
    _point = point;
   UIWindow *win = [UIApplication sharedApplication].windows.lastObject;
   [win addSubview:self];

    //表格最终的尺寸
    CGFloat w = _tableViewW?_tableViewW:150;
      self.tableView.frame = CGRectMake(point.x - w + 22, point.y + 10, w, _titles.count * 44 - 1);

    self.tableView.transform = CGAffineTransformMakeScale(tableViewScale, tableViewScale);
    [UIView animateWithDuration:animationTime animations:^{
        self.tableView.transform = CGAffineTransformIdentity;
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
        cell.textLabel.textColor = kUIColorFromRGB(0x0d0d0d);
        cell.backgroundColor = [UIColor clearColor];
        //选中时候的颜色
        UIView *backView= [UIView new];
        backView.backgroundColor = kUIColorFromRGB(0xf0f0f0);
        cell.selectedBackgroundView = backView;
    }
    cell.textLabel.text = _titles[indexPath.row];
    if (_images)//有图片，则分割线就不靠边
    {
        cell.imageView.image = [UIImage imageNamed:_images[indexPath.row]];
     
    }else//分割线靠边
    {
       if ([cell respondsToSelector:@selector(setSeparatorInset:)])
            cell.separatorInset = UIEdgeInsetsMake(0, -30, 0, 0);
       if ([cell respondsToSelector:@selector(setLayoutMargins:)])
            cell.layoutMargins = UIEdgeInsetsZero;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellClickBlock)
        self.cellClickBlock(indexPath);
    [self dismiss];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:animationTime animations:^{
        self.tableView.transform = CGAffineTransformMakeScale(0.4, 0.4);
    } completion:^(BOOL finished) {
        [self dismiss];
    }];
}

- (void)dismiss
{
    [self removeFromSuperview];
}

+ (YJPopMenuView *)popMenuViewWithTitles:(NSArray *)titles images:(NSArray *)images
{
    return [[self alloc]initWithFrame:CGRectZero titles:titles images:images];
}


@end
